package com.cc.demo2;

import com.cc.mapper.GoodMapper;
import com.dy.cc.Good;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import java.io.IOException;
import java.util.List;

//查询索引中的所有数据
public class GetClient1 {

    public static  void main(String[] args) throws IOException {
        //创建java和es的链接
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);
        RestHighLevelClient client = new RestHighLevelClient(builder);

        //查询根据文档id某个文档对象  get /index/_doc/1
        GetRequest getRequest = new GetRequest().index("good").id("1");

        //发送请求
        GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);

        //输出响应结果
        System.out.println("_index:"+ response.getIndex());
        System.out.println("_id:"+ response.getId());
        System.out.println("_type:"+ response.getType());
        String asString = response.getSourceAsString();
        System.out.println("对象数据:"+asString);




        //关闭连接
        client.close();
    }
}
